﻿//Security.loadPolicyFile("http://tada.mcmaster.ca/wikita/pub/Main/FlashTAT/crossdomain.xml");


//Packages to Import
import fl.controls.Button;
import fl.controls.DataGrid;
import fl.controls.Label;
import fl.controls.List;
import fl.controls.TextArea;

import fl.data.DataProvider;

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageScaleMode;

import flash.events.*;

import flash.external.*;

import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLVariables;

import flash.text.TextField;
import flash.text.TextFieldAutoSize;

import fl.transitions.*;
import fl.transitions.easing.*;
import fl.events.ListEvent; 

//sets scalemode, so that size stay the same regardless of stage proportions
stage.scaleMode = StageScaleMode.NO_SCALE;
  var debug = true;
//add Categories
	
//Define permanent variables
var i:int;
var sampleItem:Object;
var tool:MovieClip;
var options:MovieClip ;
var advOptions:MovieClip;
var Results:MovieClip;
var aboutCat:MovieClip;
var labelField:TextField = new TextField();
		labelField.multiline=true;
		labelField.wordWrap = true;

var patternField:TextField = new TextField();
var patternSelect:TextArea = new TextArea();
var toolInfo:TextField = new TextField();
		toolInfo.multiline=true;
		toolInfo.wordWrap = true;
var totalCats:int = 0;

	//Variables of Category headers
	var CategoriesArray:Array = new Array();
		
		tool = new CategoryHeader();
		addChild(tool);
		CategoriesArray["tool"] = {title:"Tools", object: tool, tool:openTools, position:1};
				
		Results = new CategoryHeader();
		addChild(Results);
		CategoriesArray["Results"] = {title:"Results", object: Results, tool:openResults, position:2};
		
		aboutCat = new CategoryHeader();
		addChild( aboutCat);
		CategoriesArray["about"] = {title:"About", object: aboutCat, tool:openAbout, position:3};
	//Count number of Categories
	for (var catHeadName:String in CategoriesArray){
		totalCats+=1;
		}
	var toolWidth = stage.stageWidth;
	var widthDif = 200-toolWidth; //difference between the rendered stage and the stage set on the webpage
	var toolHeight = stage.stageHeight;
	var heightDif = 400-toolHeight;
	var distanceBetween:Number=16;
	var endPoint;
	var toolTop:Number=heightDif/2;
	var toolEnd:Number=(stage.stageHeight-(distanceBetween*totalCats))+toolTop;
	var backgroundColor=0x333333;
	var catColor=0xFF9900;
	var resultsColor=0x0EC5FF;
	var linkColor = "#00FFF0";
	//Label Text Style
	var labelFormat:TextFormat;
	labelFormat = new TextFormat();
	labelFormat.font = "Myriad Pro";
	labelFormat.color = 0xFFFFFF;
	labelFormat.size = 11;
	
	var datagridPadding = 10;

//NETWORK VARIABLES
	var pageURL:String;
	var currentCat:int;
	var variables:URLVariables=new URLVariables;
	//include 'OptionsSelect2.6.1.as';
	
	
//EXTERNAL INTERFACE VARIABLES

//Creates the custom shapes for the specified category object
function formColors (object:Object, bgColor:int, catbarColor:uint) {
	//this creates the background sized square that is in every instance
	//of the category object
	var square:Shape = new Shape();
	square.graphics.beginFill(bgColor, 1);
	square.graphics.drawRect(0-((toolWidth-200)/2), 0-((toolHeight-400)/2), toolWidth, toolHeight);
	square.graphics.endFill();
	square.y=(-toolTop);
	object.bgRect.addChild(square);
	
	//this creates the category header
	//object.backg.setChildIndex (square, 0) 
	var bar:Shape = new Shape();
	bar.graphics.beginFill(catbarColor, 1);
	bar.graphics.drawRect(0-((toolWidth-200)/2), 0, toolWidth, 15);
	bar.graphics.endFill();
	object.catBar.catColor.addChild(bar);
}

//CREATE THE COLOURED OBJECTS  
function setupColors() {
	//var backgroundColor="0x" + "33CC33";
	//var catColor="0x" + "FF9999";
	//var resultsColor="0x" + "0EC5EE";
	//overall background
	var square:Shape = new Shape();
	square.graphics.beginFill(backgroundColor, 1);
	square.graphics.drawRect(0-((toolWidth-200)/2),  0-((toolHeight-400)/2), toolWidth, toolHeight);
	square.graphics.endFill();
	this.addChild(square);
	this.setChildIndex (square, 0) 
	
	//everything else
	for (var catHeadName:String in CategoriesArray)	{
		if (catHeadName=="Results") {
			formColors (CategoriesArray[catHeadName].object, backgroundColor, resultsColor);
		} else {
			formColors (CategoriesArray[catHeadName].object, backgroundColor, catColor);
		}
	}
	
	
}


if (CategoriesArray.tool) {
	//create List component
	var ListToolsField:TextField = new TextField();
	var ListTools:List = new List();
	placeComponent(ListTools, ListToolsField, "Choose Tool", datagridPadding, tool.backg);

	//Create "Select Tool" list
	var myDataProvider:DataProvider = new DataProvider();
	myDataProvider.addItem({label:"Selection 1"});
	myDataProvider.addItem({label:"Selection 2"});
	ListTools.dataProvider=myDataProvider;
	ListTools.selectedIndex=0;
	ListTools.getItemAt(0).selectable=false;
	ListTools.height=myDataProvider.length*20;
}
if (CategoriesArray.Results) {
var Loading:MovieClip = new LoadAnimation();
Loading.x=toolWidth/2+widthDif/2;
Loading.y=toolHeight/2;
Loading.height=Loading.height*0.3;
Loading.width=Loading.width*0.3;
Loading.blendMode=BlendMode.DIFFERENCE;
Results.addChild(Loading);
}
if (CategoriesArray.about) {
	//create List component
	var aboutField1:TextField = new TextField();
	aboutField1.wordWrap=true;
	aboutField1.multiline=true;
	var aboutText1:String= "<font size='-3' color='"+catColor+"'>" 
	+ link("TAToo","http://tada.mcmaster.ca/Main/FlashTAT") 
	+ " is a Flash widget which calls basic text analysis tools from the " 
	+ link("TAPoR","http://tapor.ualberta.ca/") 
	+ " project. <br/><br/>TAToo was developed by " 
	+ link("Peter Organisciak","http://www.porganized.com") 
	+ " under the supervision of " 
	+ link("Geoffrey Rockwell","http://geoffreyrockwell.com") 
	+ ".<br/><br/>TAToo is part of the"
	+ link("FlashTAT project", "http://tada.mcmaster.ca/Main/FlashTAT")
	+ ".</font>";
	
	setLabelText(aboutField1,aboutText1, datagridPadding, aboutCat.backg);
	aboutField1.height = (toolHeight-(distanceBetween*totalCats+datagridPadding*2));
}
function link(text:String, url:String) {
return "<a href='" + url + "'><font size='+1' color='"+linkColor+"'><b>" + text + "</b></font></a>";
}
function placeBelow(objectAbove) {
return objectAbove.y+objectAbove.height+datagridPadding;
}
function setLabelText(textField:TextField, textString:String, yPos:int, parent:Object) {
textField.width=toolWidth-datagridPadding*2;
textField.x=datagridPadding+(widthDif/2);
textField.y=yPos;
textField.selectable=false;
textField.defaultTextFormat=labelFormat;
textField.antiAliasType=AntiAliasType.ADVANCED;
textField.htmlText=textString;
parent.addChild(textField);
}
function placeComponent(component, textField:TextField, textString:String, yPos:int, parent:Object) {
setLabelText(textField, textString, yPos, parent);
component.move(datagridPadding+(widthDif/2),textField.y+textField.textHeight+3);
component.width=toolWidth-datagridPadding*2;
parent.addChild(component);
}
function categoryFirstRun() {
for (var catHeadName:String in CategoriesArray) {
	CategoriesArray[catHeadName].object.y=toolEnd+(CategoriesArray[catHeadName].position-1)*distanceBetween;
	catSetup(CategoriesArray[catHeadName].object, CategoriesArray[catHeadName].title, CategoriesArray[catHeadName].position-1, CategoriesArray[catHeadName].tool);
}
slidingSetup(CategoriesArray["tool"].position);

}
function catBgVisibility(BgLoading:Boolean) {
Loading.visible=BgLoading;
}
; ;
function catSetup(object:Object, barText:String, barNumber:Number, clickFunction) {
object.y=toolEnd+(distanceBetween*barNumber);
object.catBar.catTXT.text=barText;
object.catBar.useHandCursor=true;
object.addEventListener(MouseEvent.CLICK,clickFunction);
object.catBar.catShading.width=toolWidth;
object.catBar.catShading.x=widthDif/2;
}
function openTools(event:MouseEvent):void {
slidingSetup(CategoriesArray["tool"].position);
}
function openResults(event:MouseEvent):void {
slidingSetup(CategoriesArray["Results"].position);
}
function openAbout(event:MouseEvent):void {
slidingSetup(CategoriesArray["about"].position);
}
function slidingSetup(position) {
for (var catHeadName:String in CategoriesArray) {
	if (CategoriesArray[catHeadName].position<=position) {
		slideCat(CategoriesArray[catHeadName].object, toolTop+(distanceBetween*(CategoriesArray[catHeadName].position-1)));
	} else {
		slideCat(CategoriesArray[catHeadName].object, toolEnd+(distanceBetween*(CategoriesArray[catHeadName].position-1)));
	}
	currentCat=position;
}
}
function slideCat(object:Object, yCoordinate:Number) {
var myTween:Tween=new Tween(object,"y",Regular.easeIn,object.y,yCoordinate,1,true);
myTween.looping=false;
}


function changeBackground (color1, color2, color3, color4) {
 	backgroundColor="0x" + color1;
	catColor="0x" + color2;
	resultsColor="0x" + color3;
	linkColor = "#" + color4;
	setupColors();
}

var instance:Object = null;
 ExternalInterface.addCallback("sendColoursToFlash", changeBackground); 

setupColors();
categoryFirstRun();

